#include <iconv.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

/*dont set this value less than 6*/
#define ICONV_SIMPLE_BUFFER_SIZE 1024
char *simple_iconv(const char *to, const char *from, char *input, size_t input_len) {
    iconv_t *cd=iconv_open(to,from);
    char *output_result=malloc(1);
    size_t output_len=1;

    char *output_buffer_o;
    char *output_buffer;
    size_t output_buffer_left;

    size_t input_left=input_len;
    size_t increase=0;
    output_buffer_o=malloc(ICONV_SIMPLE_BUFFER_SIZE);

    while (input_left>0) {
        output_buffer=output_buffer_o;
        output_buffer_left=ICONV_SIMPLE_BUFFER_SIZE;
        iconv(cd,&input,&input_left,&output_buffer,&output_buffer_left);

        if (errno && errno!=E2BIG) {
            break;
        }

        increase=ICONV_SIMPLE_BUFFER_SIZE-output_buffer_left;
        output_len+=increase;
        output_result=realloc(output_result,output_len);
        memcpy(output_result+(output_len-increase-1),output_buffer_o,increase);
    }
    free(output_buffer_o);
    output_result[output_len]='\0';
    return output_result;

}
/*
#include <stdio.h>
char *input="是不是阿不a";
char *output=simple_iconv("UTF-8", "GBK//IGNORE", input, 11);
printf("%d,%s,%s\n",errno,output,input);
*/
